package com.guoyh.baseutils.utils

import com.blankj.utilcode.util.StringUtils
import java.lang.AssertionError
import java.lang.StringBuilder
import java.util.*

/**
 * @CreateDate: 2021/8/10 9:18 上午
 * @Author: guoyh
 * @Description: java类作用描述
 */
object RandomUtils {

    const val NUMBERS_AND_LETTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    const val NUMBERS = "0123456789"
    const val LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    const val CAPITAL_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    const val LOWER_CASE_LETTERS = "abcdefghijklmnopqrstuvwxyz"

    private fun RandomUtils() {
        throw AssertionError()
    }

    /**
     * get a fixed-length random string, its a mixture of uppercase, lowercase letters and numbers
     *
     * @param length
     * @return
     * @see RandomUtils.getRandom
     */
    fun getRandomNumbersAndLetters(length: Int): String {
        val randomStr = getRandom(NUMBERS_AND_LETTERS, length)

        return if (randomStr.isNullOrEmpty()) "${System.currentTimeMillis()}+12" else randomStr
    }

    /**
     * get a fixed-length random string, its a mixture of numbers
     *
     * @param length
     * @return
     * @see RandomUtils.getRandom
     */
    fun getRandomNumbers(length: Int): String? {
        return getRandom(NUMBERS, length)
    }

    /**
     * get a fixed-length random string, its a mixture of uppercase and lowercase letters
     *
     * @param length
     * @return
     * @see RandomUtils.getRandom
     */
    fun getRandomLetters(length: Int): String? {
        return getRandom(LETTERS, length)
    }

    /**
     * get a fixed-length random string, its a mixture of uppercase letters
     *
     * @param length
     * @return
     * @see RandomUtils.getRandom
     */
    fun getRandomCapitalLetters(length: Int): String? {
        return getRandom(CAPITAL_LETTERS, length)
    }

    /**
     * get a fixed-length random string, its a mixture of lowercase letters
     *
     * @param length
     * @return
     * @see RandomUtils.getRandom
     */
    fun getRandomLowerCaseLetters(length: Int): String? {
        return getRandom(LOWER_CASE_LETTERS, length)
    }

    /**
     * get a fixed-length random string, its a mixture of chars in source
     *
     * @param source
     * @param length
     * @return
     *  * if source is null or empty, return null
     *  * else see [RandomUtils.getRandom]
     *
     */
    fun getRandom(source: String, length: Int): String? {
        return if (StringUtils.isEmpty(source)) null else getRandom(source.toCharArray(), length)
    }

    /**
     * get a fixed-length random string, its a mixture of chars in sourceChar
     *
     * @param sourceChar
     * @param length
     * @return
     *  * if sourceChar is null or empty, return null
     *  * if length less than 0, return null
     *
     */
    fun getRandom(sourceChar: CharArray?, length: Int): String? {
        if (sourceChar == null || sourceChar.size == 0 || length < 0) {
            return null
        }
        val str = StringBuilder(length)
        val random = Random()
        for (i in 0 until length) {
            str.append(sourceChar[random.nextInt(sourceChar.size)])
        }
        return str.toString()
    }

    /**
     * get random int between 0 and max
     *
     * @param max
     * @return
     *  * if max <= 0, return 0
     *  * else return random int between 0 and max
     *
     */
    fun getRandom(max: Int): Int {
        return getRandom(0, max)
    }

    /**
     * get random int between min and max
     *
     * @param min
     * @param max
     * @return
     *  * if min > max, return 0
     *  * if min == max, return min
     *  * else return random int between min and max
     */
    fun getRandom(min: Int, max: Int): Int {
        if (min > max) {
            return 0
        }
        return if (min == max) {
            min
        } else min + Random().nextInt(max - min)
    }
}